using UnityEngine;
using System.Collections;

public class SpudGun : MonoBehaviour {
	public GameObject suckedUp = null;
	public Transform barrelpoint = null;
	public bool debug = false;
	public float force = 1000.0f;
	private int oldLayer;
	private bool full = false;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	public void Fill(GameObject obj){
		if(!full){
			if(obj.GetComponent<Spuddable>() != null){
				//obj.GetComponent<Spuddable>().PickUp();
				suckedUp = obj;	
				full = true;
				oldLayer = suckedUp.layer;
				suckedUp.layer = 11;
				suckedUp.transform.parent = gameObject.transform;
				suckedUp.transform.position = barrelpoint.position;
				suckedUp.transform.rotation = barrelpoint.rotation;
				suckedUp.rigidbody.isKinematic = true;
				suckedUp.collider.enabled = false;
				if(debug) Debug.Log("Locked and loaded");
			}
		}
		else{if(debug) Debug.Log("The Spudgun is already full, boss!");}
	}
	public void FireSnake(){
		if(full){
			if(suckedUp.GetComponent<Spuddable>() != null){
				suckedUp.transform.parent = null;
				suckedUp.rigidbody.isKinematic = false;
				suckedUp.rigidbody.AddForce(barrelpoint.forward * force);
				suckedUp.GetComponent<Spuddable>().LetOut();
				suckedUp.rigidbody.freezeRotation = true;
				suckedUp.layer = oldLayer;
				suckedUp.collider.enabled = true;
				oldLayer = 0;
				suckedUp = null;
				full = false;
			}
			else{
				if(debug) Debug.Log("The spudgun is empty");	
			}
		}
	}
	public bool getStatus(){return !full;}
}
